import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner ;

public class Manager  {
    private String name;
    private String code;


    public Manager() {
        name = "admin";
        code = "ynuinfo#777";
    }

    public void setMgrCode(String code) {
        this.code = code;
    }

    public void login(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("**********管理员登录**********");
        System.out.println("请输入用户名（默认为admin）：");
        while (!this.name.equals(reader.next()))
            System.out.println("用户名错误，请重新输入：");
        System.out.println("请输入密码（默认为ynuinfo#777）：");
        while (!this.code.equals(reader.next()))
            System.out.println("密码错误，请重新输入：");
        System.out.println("登录成功");
        Mgrmenu(customers, products);
    }

    public void Mgrmenu(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请选择操作（输入序号）：1.密码管理 2.客户管理 3.商品管理 4.退出登录");
        switch (reader.nextInt()) {
            case 1:
                manageCode(customers, products);
                break;
            case 2:
                manageCus(customers, products);
                break;
            case 3:
                managePro(customers, products);
                break;
            case 4:
                Start newstart=new Start() ;
                newstart.menu(customers,products) ;
                break;
        }
    }

    public void manageCode(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("--------------------管理密码--------------------");
        System.out.println("请选择操作（输入序号）：1.修改管理员密码 2.重置客户密码");
        switch (reader.nextInt()) {
            case 1:
                setManageCode(customers, products);
                break;
            case 2:
                resetCusCode(customers, products);
                break;
        }
    }

    //修改管理员密码
    private void setManageCode(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入新密码：");
        setMgrCode(reader.next());
        System.out.println("修改成功！");
        Mgrmenu(customers, products) ;
    }

    //重置客户密码
    private void resetCusCode(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入要重置密码的客户编号：");
        int input = reader.nextInt();
        Customer customerToReset = null;
        for (Customer customer : customers) {
            if (customer.getID() == input) {
                customerToReset = customer;
                break;
            }
        }

        // 如果找到客户，重置密码
        if (customerToReset != null) {
            System.out.println("请输入新的密码：");
            String newPassword = reader.next();
            customerToReset.setCode(newPassword) ;
            System.out.println("客户密码已重置成功！");
        } else {
            System.out.println("未找到该客户编号。");
        }
        Mgrmenu( customers, products);
    }



    public void manageCus(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("--------------------管理客户--------------------");
        System.out.println("请选择操作（输入序号）：1.客户信息列表 2.删除客户信息 3.查询客户信息 ");
        switch (reader.nextInt()) {
            case 1:
                listCus(customers, products);
                break;
            case 2:
                deleteCus(customers, products);
                break;
            case 3:
                inquireCus(customers, products);
                break;
        }
    }

    //客户信息列表
    private void listCus(ArrayList<Customer> customers, ArrayList<Product> products) {
        for (Object obj : customers)
            System.out.println(obj);
        returnMenu(customers, products);
    }

    //删除客户信息
    private void deleteCus(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入要删除的客户编号：");
        int input = reader.nextInt();
        // 检查客户编号是否有效
        if (input < 1 || input > customers.size()) {
            System.out.println("无效的客户编号。");
            return;
        }

        System.out.println("警告：删除后无法恢复，是否继续？(y/n)");
        if (Objects.equals(reader.next(), "y")) {
            customers.remove(input - 1); // 删除客户
            System.out.println("删除成功");
        } else {
            System.out.println("删除操作已取消");
        }

        returnMenu(customers, products);
    }

    //查询客户信息
    private void inquireCus(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入要查询的客户编号：");
        int input = reader.nextInt();
            // 检查客户编号是否有效
            if (input < 1 || input > customers.size()) {
                System.out.println("无效的客户编号。");
            } else {
                Customer customer = customers.get(input - 1); // 获取客户信息
                // 假设Customer类有toString方法来打印客户信息
                System.out.println("客户信息: " + customer.toString());
            }
        returnMenu(customers, products);
    }





    public void managePro(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("--------------------管理商品--------------------");
        System.out.println("请选择操作（输入序号）：1.商品信息列表 2.添加商品 3.删除商品 4.查询商品 5.修改商品");
        switch (reader.nextInt()) {
            case 1:
                listPro(customers, products);
                break;
            case 2:
                addPro(customers, products);
                break;
            case 3:
                deletePro(customers, products);
                break;
            case 4:
                inquirePro(customers, products);
                break;
            case 5:
                setPro(customers,products);
                break;
        }
    }

    //商品信息列表
    public void listPro (ArrayList<Customer> customers, ArrayList<Product> products){
        for (Object obj : products)
            System.out.println(obj);
        returnMenu(customers, products);
    }

    //添加商品
    public void addPro(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请添加商品编号：");
        int number = reader.nextInt();
        System.out.println("请添加商品名称：");
        String name = reader.next();
        System.out.println("请添加商品产家：");
        String manufacturer = reader.next();
        System.out.println("请添加商品生产日期：");
        String date = reader.next();
        System.out.println("请添加商品类型：");
        String model = reader.next();
        System.out.println("请添加商品进价：");
        int buyprice = reader.nextInt();
        System.out.println("请添加商品零售价：");
        int sellprice = reader.nextInt();
        System.out.println("请添加商品数量：");
        int amount = reader.nextInt();

        products.add(new Product(name, number, manufacturer, date, model, buyprice, sellprice, amount));
        returnMenu(customers, products);
    }

    //删除商品
    public void deletePro (ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入要删除的商品编号：");
        int input = reader.nextInt();
        // 检查输入的商品编号是否有效
        if (input < 1 || input > products.size()) {
            System.out.println("无效的商品编号，请输入有效的编号。");
            return;
        }

        System.out.println("警告：删除后无法恢复，是否继续？(y/n)");
        if (Objects.equals(reader.next(), "y")) {
            products.remove(input - 1);
            System.out.println("删除成功");
        } else {
            System.out.println("删除操作已取消");
        }
        returnMenu(customers, products);
    }

//查询商品
        // 可以根据商品名称、生产厂家、零售价格进行单独查询或者组合查询（例如：查询联想公司、1000以上的商品清单
        public void inquirePro (ArrayList<Customer> customers, ArrayList<Product> products) {
            Scanner reader = new Scanner(System.in);
            System.out.println("请输入要查询的商品名称、生产厂家（或输入'无'跳过）、零售价格（或输入'0'跳过）");

            String productName = reader.nextLine();
            String manufacturer = reader.nextLine();
            double retailPrice = 0;
            try {
                retailPrice = Double.parseDouble(reader.nextLine());
            } catch (NumberFormatException e) {
                retailPrice = 0; // 如果输入不是数字，保持为0
            }

            ArrayList<Product> filteredProducts = new ArrayList<>();

            for (Product product : products) {
                boolean matches = true;

                if (!productName.equals("无") && !product.getName().contains(productName)) {
                    matches = false;
                }
                if (!manufacturer.equals("无") && !product.getManufacturer().equals(manufacturer)) {
                    matches = false;
                }
                if (retailPrice != 0 && product.getSellPrice() < retailPrice) {
                    matches = false;
                }

                if (matches) {
                    filteredProducts.add(product);
                }
            }

            if (filteredProducts.isEmpty()) {
                System.out.println("没有找到符合条件的商品。");
            } else {
                System.out.println("符合条件的商品清单：");
                for (Product product : filteredProducts) {
                    System.out.println(product);
                }
            }
            returnMenu(customers, products);
        }

        //修改商品信息
    public void setPro(ArrayList<Customer> customers, ArrayList<Product> products) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入要修改的商品编号：");
        int index = reader.nextInt();
            System.out.println("请选择需要修改的商品信息：1.编号 2.名称 3.产家 4.生产日期 5.进价 6.零售价");
            int input = reader.nextInt();
            switch (input) {
                case 1:
                    System.out.println("请输入新的商品编号：");
                    int newnumber = reader.nextInt();
                    Product product1 = products.get(index-1);
                    product1.setNumber(newnumber ) ;
                    break;
                case 2:
                    System.out.println("请输入新的商品名称：");
                    String  newname = reader.next();
                    Product product2 = products.get(index-1);
                    product2.setName(newname) ;
                    break;
                case 3:
                    System.out.println("请输入新的商品产家：");
                    String  newmanufacturer = reader.next();
                    Product product3 = products.get(index-1);
                    product3.setManufacturer(newmanufacturer) ;
                    break;
                case 4:
                    System.out.println("请输入新的商品生产日期：");
                    String  newdate = reader.next();
                    Product product4 =products.get(index-1);
                    product4.setManufacturer(newdate) ;
                    break;
                case 5:
                    System.out.println("请输入新的商品进价：");
                    int  newbuyprice = reader.nextInt() ;
                    Product product5 = products.get(index-1);
                    product5.setBuyPrice(newbuyprice) ;
                    break;
                case 6:
                    System.out.println("请输入新的商品零售价：");
                    int  newsellprice = reader.nextInt() ;
                    Product product6 =  products.get(index-1);
                    product6.setSellPrice(newsellprice) ;
                    break;
            }
            returnMenu(customers, products);
        }



        public void returnMenu(ArrayList<Customer> customers, ArrayList<Product> products){
            Scanner reader = new Scanner(System.in);
            System.out.println("输入0返回菜单");
            if (reader.nextInt() == 0) {
                this.Mgrmenu(customers, products);
            }
        }

    }


